<?php
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph.php';
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph_bar.php';

/**
 * JPGraph Bar Chart Wrapper
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class Nina_Graph_Jpgraph_Bar extends Nina_Graph_Jpgraph_Plot
{
	/**
	 * Constructor
	 * @param integer [$width=400] The width
	 * @param integer [$height=400] The height
	 */
	public function __construct($width = 400, $height = 400) {
		$this->graph = new Graph($width, $height);
		$this->graph->setMargin(50, 10, 10, 40);
		$this->plot = new BarPlotExtended();

		parent::__construct();
	}

	/**
	 * Set the bar alignment
	 * @param string [$align='center'] The alignment
	 */
	public function setAlign($align = 'center') {
		$this->plot->SetAlign($align);
	}

	/**
	 * Set a fill color (or set of colors) for the bars
	 * @param string|array|false [$color=false] The color(s) to fill the bars with
	 */
	public function setFillColor($color = false) {
		if($color)
			$this->plot->SetFillColor($color);
		else
			$this->plot->SetNoFill();
	}

	/**
	 * Set individual outline colors for the bars
	 * @param array $colors Array of colors
	 */
	public function setOutlines($colors) {
		$this->plot->SetColor($colors);
	}

	/**
	 * Set a fill gradient for the bars
	 * @param string $from_color The color for the start of the gradient
	 * @param string [$to_color=null] The color for the end of the gradient
	 */
	public function setFillGradient($from_color, $to_color = null) {
		$this->plot->setFillGradient($from_color, $to_color = null);
	}

	/**
	 * Make the bars 3D
	 * @param integer [$width=3] Width
	 * @param integer [$depth=3] Depth
	 */
	public function set3D($width = 3,$depth = 3) {
		$this->plot->Set3D($width, $depth, true);
	}
}

/**
 * JPGraph Bar Chart Extension
 * Adds ability to set data outside of the constructor
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class BarPlotExtended extends BarPlot {

	/**
	 * Parent constructor doesn't work unless we start with some data
	 * @param array [$data=null] The data
	 */
	public function __construct($data = null) {
		if(!$data)
			$data = array(1);

		parent::__construct($data);
	}

	/**
	 * Set the plot data
	 * @param array $data The data
	 */
	public function setData($data) {
		$this->inputValues = array();
		$this->coords[0] = $data;
		$this->inputValues['aDatay'] = $data;
		$this->inputValues['aDatax'] = false;
		$this->numpoints = count($data) + 1;
	}
}
